package com.icez.base_util_library

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment

/**
 *  权限工具类
 * @author icez
 */
class PermissionUtil {
    companion object {
        val instance: PermissionUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            PermissionUtil()
        }
    }

    /**
     *  申请权限
     * @param fragment
     * @param listPermissions 申请的权限
     * @param onPermissionListener 权限申请回调
     */
    public fun requestPermission(
        fragment: Fragment,
        listPermissions: ArrayList<String>,
        onPermissionListener: ((agreeQermissions: ArrayList<String>, refuseQermissions: ArrayList<String>) -> Unit)? = null
    ) {
        val agreeQermissions = ArrayList<String>()
        val refuseQermissions = ArrayList<String>()
        fragment.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions: Map<String, Boolean> ->
            permissions.entries.forEachIndexed { index, entry ->
                if (entry.value) {
                    agreeQermissions.add(entry.key)
                } else {
                    refuseQermissions.add(entry.key)
                }
            }
            if (onPermissionListener != null) {
                onPermissionListener(agreeQermissions, refuseQermissions)
            }
        }.launch(
            listPermissions.toTypedArray()
        )
    }

    /**
     *  申请权限
     * @param activity
     * @param listPermissions 申请的权限
     * @param onPermissionListener 权限申请回调
     */
    public fun requestPermission(
        activity: AppCompatActivity,
        listPermissions: ArrayList<String>,
        onPermissionListener: ((agreeQermissions: ArrayList<String>, refuseQermissions: ArrayList<String>) -> Unit)? = null
    ) {
        val agreeQermissions = ArrayList<String>()
        val refuseQermissions = ArrayList<String>()
        activity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions: Map<String, Boolean> ->
            permissions.entries.forEachIndexed { index, entry ->
                if (entry.value) {
                    agreeQermissions.add(entry.key)
                } else {
                    refuseQermissions.add(entry.key)
                }
            }
            if (onPermissionListener != null) {
                onPermissionListener(agreeQermissions, refuseQermissions)
            }
        }.launch(
            listPermissions.toTypedArray()
        )
    }

    /**
     *  检查权限是否已经申请
     * @param context 上下文
     * @param mapPermissions 需要申请的权限 例如：mapPermissions.put("Manifest.permission.REQUESTED_PERMISSION",PackageManager.PERMISSION_GRANTED)
     */
    public fun checkPermissions(context: Context, mapPermissions: HashMap<String,Int>): Boolean {
        var isCheckPass = true
        mapPermissions.entries.forEachIndexed { index, mutableEntry ->
            if (ContextCompat.checkSelfPermission(
                    context,
                    mutableEntry.key
                ) != mutableEntry.value
            ) {
                isCheckPass = false
                return isCheckPass
            }
        }

        return isCheckPass

    }

    /**
     *  去开启悬浮窗权限
     * @param context 上下文
     */
    public fun toOpenOverlayUI(context:Context){
        val intent = Intent().apply {
            action = Settings.ACTION_MANAGE_OVERLAY_PERMISSION
        }
        context.startActivity(intent)
    }


}